package com.groupbuying.store.controller.agent;

import java.util.List;
import java.util.Map;

import com.groupbuying.store.constants.StoreAccountEnumConstants;
import com.groupbuying.store.domain.agent.StoreAgentAccountDO;
import com.groupbuying.store.service.agent.StoreAgentAccountService;
import com.groupbuying.store.utils.*;
import com.groupbuying.store.utils.apiresult.ApiResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 服务商账号表
 * 
 * @author WANGNIGN
 * @email wyn20140226@aliyun.com
 * @date 2019-07-23 10:24:23
 */
 
@RestController
@RequestMapping("/agent/store/storeAccount")
public class StoreAgentAccountController {
	@Autowired
	private StoreAgentAccountService storeAccountService;
	public static final Logger logger = LoggerFactory.getLogger(StoreAgentAccountController.class);

	@GetMapping()
	@RequiresPermissions("agent:storeAccount:storeAccount")
	String StoreAccount(){
	    return "agent/storeAccount/storeAccount";
	}
	
	@GetMapping("/list")
	@RequiresPermissions("agent:storeAccount:storeAccount")
	public PageUtils list(@RequestParam Map<String, Object> params){
		logger.info("StoreAgentAccountController.params()----->通过代理商id添加服务商获取的列表参数:"+params.toString());
		//查询列表数据
        Query query = new Query(params);
		List<StoreAgentAccountDO> storeAccountList = storeAccountService.list(query);
		for(StoreAgentAccountDO StoreAgentAccountDO :storeAccountList){
			String status = StoreAgentAccountDO.getStatus();
			if(StoreAccountEnumConstants.ENUM_STATUS_NORMAL.equals(status)){
				StoreAgentAccountDO.setStatus("审核通过");
			}else if(StoreAccountEnumConstants.ENUM_STATUS_WAIT.equals(status)){
				StoreAgentAccountDO.setStatus("待审核");
			}else if(StoreAccountEnumConstants.ENUM_STATUS_REJECT.equals(status)){
				StoreAgentAccountDO.setStatus("审核驳回");
			}
		}
		int total = storeAccountService.count(query);
		PageUtils pageUtils = new PageUtils(storeAccountList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("agent:storeAccount:add")
	String add(){
	    return "admin/storeAccount/add";
	}

	@GetMapping("/edit/{accountId}")
	@RequiresPermissions("agent:storeAccount:edit")
	String edit(@PathVariable("accountId") String accountId,Model model){
		StoreAgentAccountDO storeAccount = storeAccountService.get(accountId);
		model.addAttribute("storeAccount", storeAccount);
	    return "admin/storeAccount/edit";
	}
	
	/**
	 * 保存
	 */

	@RequestMapping(value = { "/save" }, produces = { "text/html;charset=UTF-8" }, method = {
			RequestMethod.POST })
	public R save(@RequestParam Map<String, Object> params){
		logger.info("StoreAgentAccountController.save()----->获取到代理商信息进行服务商的添加，参数信息为:"+params.toString());
		//获取参数信息
		String accountName = (String)params.get("accountName");
		String password = (String)params.get("password");
		String name = (String)params.get("name");
		String email = (String)params.get("email");
		String accountNo = (String)params.get("accountNo");
		String providerName = (String)params.get("providerName");
		String providerDes = (String)params.get("providerDes");
		/*获取到的代理商id*/
		String agentId = (String)params.get("agentId");
		StoreAgentAccountDO storeAccount=new StoreAgentAccountDO();
		storeAccount.setAccountName(accountName);
		storeAccount.setPassword(password);
		storeAccount.setName(name);
		storeAccount.setEmail(email);
		storeAccount.setAccountNo(accountNo);
		storeAccount.setProviderName(providerName);
		storeAccount.setProviderDes(providerDes);
		int res = this.storeAccountService.save(storeAccount);
		if(res>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("admin:storeAccount:edit")
	public R update( StoreAgentAccountDO storeAccount){
		storeAccountService.update(storeAccount);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@RequiresPermissions("admin:storeAccount:remove")
	public R remove( String accountId){
		if(storeAccountService.remove(accountId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@RequiresPermissions("admin:storeAccount:batchRemove")
	public R remove(@RequestParam("ids[]") String[] accountIds){
		storeAccountService.batchRemove(accountIds);
		return R.ok();
	}
	/**
	 * 通过代理商添加服务商
	 * @param
	 * @return
	 */
	@RequestMapping("/addAccount")
	public ApiResult addAccount(@RequestParam Map<String, Object> params) {
		logger.error("StoreAgentAccountController.addAccount() params"+params.toString());
		ApiResult apiResult=this.storeAccountService.saveAccount(params);
        logger.error("StoreAgentAccountController.addAccount() result:::"+apiResult.toString());
        return apiResult;
	}

	@RequestMapping("/updateStatus")
	public ApiResult updateStatus(@RequestParam Map<String, Object> params) {
		logger.info("StoreAgentAccountController.updateStatus()----->请求代理商更改服务商状态:"+params.toString());
		params.put("providerType","0");
		int res =this.storeAccountService.updateStatus(params);
		if(res>0){
			logger.info("StoreAgentAccountController.updateStatus() 修改成功");
			return ApiResult.success();
		}else{
			logger.info("StoreAgentAccountController.updateStatus() 修改失败");
			return ApiResult.failure();
		}
	}

	/**
	 *通过代理商id查服务商门店信息
	 */
	@GetMapping("/storeMainInfoListByAgent")
	public PageUtils storeMainInfoListByAgent(@RequestParam Map<String, Object> params){
		logger.info("StoreAgentAccountController.storeMainInfoListByAgent()----->通过代理商id获取门店列表参数:"+params.toString());
		PageUtils storeMainInfoListByAgentList = storeAccountService.storeMainInfoListByAgent(params);
		return storeMainInfoListByAgentList;
	}
	/**
	 *通过代理商门店运营人员信息
	 */
	@GetMapping("/getStoreOperList")
	public PageUtils getStoreOperList(@RequestParam Map<String, Object> params){
		logger.info("StoreAgentAccountController.getStoreOperList()----->通过代理商id获取门店运营人员列表参数:"+params.toString());
		PageUtils getStoreOperList = storeAccountService.getStoreOperList(params);
		return getStoreOperList;
	}
	/**
	 *编辑代理商门店运营人员信息
	 */
	@PostMapping("/setStoreOper")
	public R setStoreOper(@RequestBody Map<String, Object> params){
		return storeAccountService.setStoreOper(params);
	}
}
